
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AjaxForWinForms
{
    /// <summary>
    /// class to have a TextBoxWatermark
    /// http://ajax.asp.net/ajaxtoolkit/TextBoxWatermark/TextBoxWatermark.aspx
    /// in Windows Forms
    /// </summary>
    public class TextBoxWatermark: TextBox
    {
        /// <summary>
        /// constructor
        /// </summary>
        public TextBoxWatermark()
            : base()
        {
            
            this.LostFocus += new EventHandler(ExtTextBox_LostFocus);
            this.GotFocus +=new EventHandler(ExtTextBox_GotFocus);
            
        }
        /// <summary>
        /// hides default implementation
        /// in order to not give worng values
        /// </summary>
        public new string Text
        {
            get
            {
                if (base.Text == WatermarkText)
                    return "";
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }
        void  ExtTextBox_GotFocus(object sender, EventArgs e)
        {
            RemoveWatermark();
        }
        void ExtTextBox_LostFocus(object sender, EventArgs e)
        {
            SetWatermark();
        }
        private Color BackColorCache;
        private Color ForeColorCache;

        private void RemoveWatermark()
        {
            if (base.Text == WatermarkText && !DesignMode)
            {
                base.Text = "";
                this.BackColor = BackColorCache;
                this.ForeColor = ForeColorCache;
            }
        
        }
        
        private void SetWatermark()
        {

            if (string.IsNullOrEmpty(this.Text) && !DesignMode)
            {
                BackColorCache = this.BackColor;
                ForeColorCache = this.ForeColor;
                base.Text = WatermarkText;
                this.BackColor = WatermarkBackColor;
                this.ForeColor = WatermarkForeColor;
            }
        }
        private string _watermarkText;

        /// <summary>
        /// the text that will appear if nothing write
        /// </summary>
        [Description("the text that will appear if nothing write")]
        [Category("Watermark")]
        public string WatermarkText
        {
            get 
            {              
                return _watermarkText; 
            }
            set 
            { 
                _watermarkText = value;
                SetWatermark();
            }
        }
        private Color _watermarkBackColor=Color.Teal;

        /// <summary>
        /// the back color to appear when watermarked
        /// </summary>
        [Description("the back color to appear when watermarked")]
        [Category("Watermark")]
        public Color WatermarkBackColor
        {
            get { return _watermarkBackColor; }
            set { _watermarkBackColor = value; }
        }

        private Color _watermarkForeColor = Color.White;


        /// <summary>
        /// the fore color to appear when watermarked
        /// </summary>
        [Description("the fore color to appear when watermarked")]
        [Category("Watermark")]
        public Color WatermarkForeColor 
        {
            get { return _watermarkForeColor; }
            set { _watermarkForeColor = value; }
        }
        
    }
}
